package com.hangsu.train.components.web.controller;

import com.hangsu.train.components.entity.User;
import com.hangsu.train.components.params.LoginParam;
import com.hangsu.train.components.service.UserService;
import com.hangsu.train.core.result.Err;
import com.hangsu.train.core.result.Resp;
import lombok.extern.slf4j.Slf4j;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;

@Slf4j
@RestController
public class IndexController {

    @Resource
    private UserService userService;

    /**
     * 手机号码、密码登录
     */
    @PostMapping("/login")
    public Resp<?> login(@RequestBody @Valid LoginParam param) throws Exception {
        User user = userService.login(param);
        if (user!=null){
            String token = userService.genToken(user);
            return Resp.ok(token);
        }
        return Resp.fail(Err.AUTHED_LOGIN_ERROR);
    }

}